<template>
  <el-dialog :visible.sync="visible" v-if="visible" :title="title">
    <el-form :model="formData" :rules="rules" ref="formRef" label-width="80px">
      <el-form-item label="部门名称" prop="departmentName">
        <el-input v-model="formData.departmentName" placeholder="请输入部门名称"/>
      </el-form-item>
      <el-form-item label="部门介绍">
        <el-input v-model="formData.profile" type="textarea" :maxlength="150" show-word-limit placeholder="请输入部门介绍"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">可用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button type="warning" @click="visible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
const formData = {
  id: '',
  departmentName: '',
  profile: '',
  status: 1
}
export default {
  name: 'departmentUpdate',
  data () {
    return {
      visible: false,
      title: '添加数据',
      formData,
      rules: {
        departmentName: [{
          required: true,
          message: '部门名不能为空'
        }]
      }
    }
  },
  methods: {
    init (id) {
      this.visible = true
      this.title = id ? '修改数据' : '添加数据'
      this.formData = { ...formData }
      if (id) {
        this.$http.get(`department/info/${id}`).then(data => {
          if (data) {
            this.formData = { ...data }
          }
        })
      }
    },
    submit () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$http.post('department/update', this.formData).then(() => {
            this.$message.success('操作成功')
            this.visible = false
            this.$emit('closeDialog')
          }).catch(() => {})
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
